#include "game.hpp"

Game::Game(Lobby * lobby, Client * client, quint8 id, quint8 gobanSize, qreal komi, const QString & black, QObject * parent) :
    QObject(parent),
    _lobby(lobby),
    _client(client),
    _id(id),
    _gobanSize(gobanSize),
    _komi(komi),
    _isStarted(false),
    _isEstablishingScore(false),
    _isFinished(false),
    _blackToPlay(true),
    _black(black)
{
    _goban = new Goban(_gobanSize, this);
    lobby->addGame(id, gobanSize, komi, black);
}

Game::Game(Lobby * lobby, Client * client, quint8 id, quint8 gobanSize, qreal komi, const QString & black, const QString & white, QObject * parent) :
    QObject(parent),
    _lobby(lobby),
    _client(client),
    _id(id),
    _gobanSize(gobanSize),
    _komi(komi),
    _blackToPlay(true),
    _black(black),
    _white(white)
{
    _goban = new Goban(_gobanSize, this);
    lobby->addGame(id, gobanSize, komi, black, white);
}

quint8 Game::id() const
{
    return _id;
}

QString Game::black() const
{
    return _black;
}

Goban * Game::goban() const
{
    return _goban;
}

QString Game::white() const
{
    return _white;
}

int Game::komi() const
{
    return _komi;
}

QStringList Game::observers() const
{
    return _observers;
}

bool Game::isStarted() const
{
    return _isStarted;
}

bool Game::isEstablishingScore() const
{
    return _isEstablishingScore;
}

void Game::setEstablishingScore(bool isEstablishingScore)
{
    _isEstablishingScore = isEstablishingScore;
}

bool Game::isFinished() const
{
    return _isFinished;
}

bool Game::blackToPlay() const
{
    return _blackToPlay;
}

void Game::switchBlackToPlay()
{
    _blackToPlay = !_blackToPlay;
}

int Game::size() const
{
    return _gobanSize;
}

void Game::addPlayer(const QString & name)
{
    _isStarted = true;
    _white = name;
    emit updatePlayer();
    _lobby->setWhite(_id, name);
}

void Game::addObserver(const QString & name)
{
    _observers.append(name);
    emit updatePlayer();
}

void Game::removeObserver(const QString & name)
{
    if (_observers.contains(name))
    {
        _observers.removeAll(name);
        emit updatePlayer();
        return;
    }
}

void Game::setState(const QStringList & observerList, const QVector < QVector<quint8> > & map)
{
    _observers = observerList;
    _goban->setMap(map);

    emit updatePlayer();
}

void Game::setObservers(const QStringList & observerList)
{
    _observers = observerList;

    emit updatePlayer();
}

void Game::clearObservers()
{
    _observers.clear();
}

void Game::destroy()
{
    _lobby->removeGame(_id);
}

